package com.efast.dmom.equipment.service.masterdata.impl;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.efast.dmom.equipment.bean.EquipHomeTaskParamBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.framework.bean.QueryPageBase;
import com.efast.cafe.portal.bean.query.QueryBean;
import com.efast.cafe.portal.bean.query.QueryConditionBean;
import com.efast.dmom.equipment.bean.EquipInterferSpecificDataBean;
import com.efast.dmom.equipment.bean.EquipMainInterferBaseBean;
import com.efast.dmom.equipment.dao.masterdata.EquipTaskPageDao;
import com.efast.dmom.equipment.service.masterdata.EquipTaskPageService;

@Service
public class EquipTaskPageServiceImpl implements EquipTaskPageService{

	@Autowired
	private EquipTaskPageDao equipTaskPageDao;

	@Override
	public EquipHomeTaskParamBean queryEquipRepairBase(EquipHomeTaskParamBean querys) {

		List<EquipMainInterferBaseBean> queryEquipRepairOrder=equipTaskPageDao.queryRepairData(querys.getStart(),querys.getLength());
     	List<String> statusList=new ArrayList<>();
		statusList.add("audited");
		Integer count=equipTaskPageDao.queryRepairAllCount(statusList);
		statusList.add("audit");
		statusList.add("todo");
		statusList.add("todoing");
		statusList.add("completed");
		Integer allcount=equipTaskPageDao.queryRepairAllCount(statusList);
		statusList.clear();
		statusList.add("todo");
		statusList.add("todoing");
		Integer pageCount=equipTaskPageDao.queryRepairAllCount(statusList);
		//维修达标率
		String repairRate="";
		if(allcount>0 ) {
			   NumberFormat numberFormat = NumberFormat.getInstance();
				numberFormat.setMaximumFractionDigits(2);
	        	repairRate=numberFormat.format((count/(allcount*1.0))*100);
		    }
		querys.setRepairRate(repairRate);
		querys.setResultList(queryEquipRepairOrder);
		querys.setTotalRows(pageCount);
		return querys;
	}

	@Override
	public EquipInterferSpecificDataBean selectRepairEquipResume(String orderNumber){
		EquipInterferSpecificDataBean selectEquipRepairOrderDetail= equipTaskPageDao.SelectRepairEquipResume(orderNumber);
		      if(selectEquipRepairOrderDetail!=null) {
				if(selectEquipRepairOrderDetail.getStatus().equals("audited")) {
					SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
					String date=formatter.format(selectEquipRepairOrderDetail.getActuralEndTime());
					selectEquipRepairOrderDetail.setStatus(date+"-已完成");
				}else {
					selectEquipRepairOrderDetail.setStatus("维修中");
				}
		}
		return selectEquipRepairOrderDetail;
	}
	
	@Override
	  public EquipHomeTaskParamBean selectEquipMaintainOrder(EquipHomeTaskParamBean querys) {
	    String format =null;
	    List<EquipMainInterferBaseBean> selectEquipMaintainOrder = equipTaskPageDao.selectEquipMaintainOrder(querys.getStart(),querys.getLength());

		List<String> statusList=new ArrayList<>();
		statusList.add("completed");
        Integer completed=equipTaskPageDao.selectEquipMaintainOrderCountByStatus(statusList);
        statusList.clear();
		statusList.add("todo");
		statusList.add("todoing");
		Integer pageCount=equipTaskPageDao.selectEquipMaintainOrderCountByStatus(statusList);
		statusList.add("audit");
		statusList.add("completed");
		Integer allCount=equipTaskPageDao.selectEquipMaintainOrderCountByStatus(statusList);

	   if(allCount>0){
			NumberFormat numberFormat = NumberFormat.getInstance();
			numberFormat.setMaximumFractionDigits(2);
			format = numberFormat.format(completed/(allCount*1.0));
	    }
		querys.setResultList(selectEquipMaintainOrder);
		querys.setTotalRows(pageCount);
		querys.setRepairRate(format);
         return querys;
	  }
	  @Override
	  public EquipInterferSpecificDataBean selectEquipMaintainOrderDetail(String ordernumber) {
	    Calendar calendar = new GregorianCalendar();
	    EquipInterferSpecificDataBean selectEquipMaintainOrderDetail = equipTaskPageDao.selectEquipMaintainOrderDetail(ordernumber);
	    calendar.setTime(selectEquipMaintainOrderDetail.getActuralEndTime());
	    SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    String format = f.format(selectEquipMaintainOrderDetail.getActuralEndTime());
	    if(selectEquipMaintainOrderDetail.getStatus().equals("completed")) {
	      selectEquipMaintainOrderDetail.setStatus((format+"").substring(0, 11)+"(已完成)");
	      String frequency = selectEquipMaintainOrderDetail.getFrequency();
	      String substring = frequency.substring(frequency.indexOf("~")+1);
	      int cycle = Integer.parseInt(selectEquipMaintainOrderDetail.getCycle());
	      if(substring.equals("day")) {
	        calendar.add(Calendar.DAY_OF_MONTH, cycle);
	      } else if(substring.equals("hour")) {
	        calendar.add(Calendar.HOUR_OF_DAY, cycle);
	      } else if(substring.equals("month")) {
	        calendar.add(Calendar.MONTH, cycle);
	      } else if(substring.equals("quarter")) {
	        calendar.add(Calendar.MONTH, cycle*3);
	      }
	      selectEquipMaintainOrderDetail.setLastPlan(format+"");
	    } else {
	      selectEquipMaintainOrderDetail.setStatus("保养中");
	      selectEquipMaintainOrderDetail.setLastPlan(null);
	    }
	    return selectEquipMaintainOrderDetail;
	  }
	  @Override
	  public EquipHomeTaskParamBean selectEquipCheckOrder(EquipHomeTaskParamBean querys) {
	    String format =null;
	    List<EquipMainInterferBaseBean> selectEquipMaintainOrder = equipTaskPageDao.selectEquipCheckOrder(querys.getStart(),querys.getLength());
		  List<String> statusList=new ArrayList<>();
		  statusList.add("completed");
		  Integer completed=equipTaskPageDao.selectEquipCheckOrderByStatus(statusList);
		  statusList.clear();
		  statusList.add("todo");
		  statusList.add("todoing");
		  Integer pageCount=equipTaskPageDao.selectEquipCheckOrderByStatus(statusList);
		  statusList.add("audit");
		  statusList.add("completed");
		  Integer allCount=equipTaskPageDao.selectEquipCheckOrderByStatus(statusList);
        if(allCount>0) {
			NumberFormat numberFormat = NumberFormat.getInstance();
			numberFormat.setMaximumFractionDigits(2);
			format = numberFormat.format(completed / (allCount* 1.0));

		}
		querys.setTotalRows(pageCount);
		querys.setResultList(selectEquipMaintainOrder);
        querys.setRepairRate(format);
	    return querys;
	  }
	  @Override
	  public EquipInterferSpecificDataBean selectEquipCheckOrderDetail(String ordernumber) {
	    Calendar calendar = new GregorianCalendar();
	    EquipInterferSpecificDataBean selectEquipCheckOrderDetail = equipTaskPageDao.selectEquipCheckOrderDetail(ordernumber);
	    String frequency = selectEquipCheckOrderDetail.getFrequency();
	    String substring = frequency.substring(frequency.indexOf("~")+1);
	    calendar.setTime(selectEquipCheckOrderDetail.getActuralEndTime());
	    SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    String format = f.format(selectEquipCheckOrderDetail.getActuralEndTime());
	    if(selectEquipCheckOrderDetail.getStatus().equals("completed")) {
	      selectEquipCheckOrderDetail.setStatus((format+"").substring(0, 11)+"(已完成)");
	      if(substring.equals("day")) {
	        calendar.add(Calendar.DAY_OF_MONTH, 1);
	      } else if(substring.equals("hour")) {
	        calendar.add(Calendar.HOUR_OF_DAY, 1);
	      } else if(substring.equals("month")) {
	        calendar.add(Calendar.MONTH, 1);
	      } else if(substring.equals("quarter")) {
	        calendar.add(Calendar.MONTH, 3);
	      }
	      selectEquipCheckOrderDetail.setLastPlan(format+"");
	    } else {
	      selectEquipCheckOrderDetail.setStatus("点检中");
	      selectEquipCheckOrderDetail.setLastPlan(null);
	    }
	    return selectEquipCheckOrderDetail;
	  }
}

